<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 * 用户相关接口
 * @author Calvin<hongbinfu@163.com>
 * @readme  :返回结果请使用common_response
 * @help    :具体用法请参考https://github.com/chriskacerguis/codeigniter-restserver
 * @other   :
 *          GET ---  $this->get();
 *          POST --- $this->post();
 *          PUT --- $this->put();
  *         DELETE --- $this->delete();
 */
require(APPPATH.'libraries/Pc_Controller.php');
class User_center extends Pc_Controller
{
    private $_find_pwd_code = "__FIND_PWD_CODE__";
    private $_find_pwd_user = "__FIND_PWD_USER__";

    public function __construct() {
        parent::__construct();
        $services = array(
            'register_service',
            'login_service',
            'user_verify_service',
            'user_info_service'
        );
        $this->load->service($services);
        $this->load->library("form_validation");
        $this->config->load("form_validation");
    }

    public function index_get()
    {
        $this->common_response(1003);
    }

    /**
     * 用户注册
     */
    public function register_post() {
        $this->form_validation->set_rules('mobile','', 'required|valid_mobile_phone');
        $this->form_validation->set_rules('password','', 'required|valid_password');
        $this->form_validation->set_rules('code','', 'required');
        if ($this->form_validation->run() === FALSE)
            return $this->common_response(1003);

        $mobile = trim($this->input->post('mobile', true));
        $password = trim($this->input->post('password',true));
        $sms_vcode = $this->input->post("code",true);

        $is_exist = $this->user_verify_service->check_user_by_phone($mobile);
        if (! empty($is_exist))
            return $this->common_response(5009);

        $check_code = $this->user_verify_service->check_sms_code($sms_vcode, $mobile, 1, TRUE);
        if (empty($check_code))
            return $this->common_response($this->user_verify_service);

        $resp = $this->register_service->register_by_phone($mobile, $password);
        if (empty($resp))
            return $this->common_response($this->register_service);

        $this->register_service->delete_register_log();
        $this->login_service->unset_tryout();
        //登录
        $this->login_service->login_by_uid($this->register_service->get_register_uid());
        return $this->common_response(0);
    }


    /**
     * ::注册用户::发送验证码
     * @return array
     */
    public function sms_code_post() {
        $this->form_validation->set_rules('mobile','', 'required|valid_mobile_phone');
        if ($this->form_validation->run() === FALSE)
            return $this->common_response(1003);

        $mobile = trim($this->input->post("mobile",true));
        $check_exist = $this->register_service->check_phone_exists($mobile);

        if ($check_exist)
            return $this->common_response(5009);

        $send = $this->user_verify_service->send_sms($mobile, User_verify_service::PROCESS_REGISTER);
        if ($send)
            return $this->common_response(0);

        return $this->common_response($this->user_verify_service);
    }

    /**
     * 用户登陆
     */
    public function login_post() {
        //清空找回密码的session
        if (isset($_SESSION[$this->_find_pwd_user])) {
            unset($_SESSION[$this->_find_pwd_code]);
            unset($_SESSION[$this->_find_pwd_user]);
        }

        $account = trim($this->input->post('account',true));
        //$password = trim($this->input->post('password',true));

        $password = $this->post('password');

        if (empty($account) || empty($password))
            return $this->common_response(1003);

        $email_valid = $this->form_validation->valid_email($account);
        $phone_valid = $this->form_validation->valid_mobile_phone($account);
        if (empty($email_valid) && empty($phone_valid))
            return $this->common_response(1003);

        //验证帐号
        $user = (true == $email_valid) ?
            $this->login_service->check_login_by_email($account, $password):
            $this->login_service->check_login_by_phone($account, $password);

        if (empty($user)) {
            $this->login_service->add_login_error_log();
            return $this->common_response($this->login_service);
        }

            $this->login_service->login_by_data($user,0,0);
            return $this->common_response(0);
    }

    /**
     * 获取登陆状态
     */
    public function login_status_get(){
        $status = $this->login_service->is_login();
        if (empty($status))
            return $this->common_response(1011);

        return $this->common_response(0);
    }

    /**
     * 获取更新
     */
    public function version_get() {
        $version = trim($this->input->get("v", true));
        if (empty($version))
            return $this->common_response(1003);

        $last_version = $this->config->item("pc_version");
        #从数据库取出最新版本
        $result = array(
            'version' => $last_version,
            'description' => <<<EOF
            1.修复保存登陆的问题；
            2.更新意见反馈功能；
            3.优化启动，软件启动得更快；
            4.更改LOGO，品牌识别度更高。
EOF
        );

        $down_pc = sprintf($this->config->item("download_format"), $last_version, "exe");
        $down_url = site_url($down_pc);

        $res = array(
            'code'          => 0,
            'version'       => $version,
            'url'           => '',
            'description'   => "暂无更新",
            'required'      => true
        );

        if (version_compare($result['version'], $version, ">")) {
            $res['code']        = 1;
            $res['version']     = $result['version'];
            $res['url']         = $down_url;
            $res['description'] = $result['description'];
        }

        return $this->common_response(0, '', array('data' => $res));
    }
    
}



